package com.chookss.home.inspection;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.home.entity.InspectionPicEntity;
import com.chookss.tools.GridSpacingItemDecoration;
import com.chookss.tools.Utils;
import com.chookss.view.DrawableTextView;

import java.util.List;

import io.reactivex.annotations.Nullable;

public class InspectionListAdapter extends BaseQuickAdapter<InspectionPicEntity, BaseViewHolder> {
    private Activity context;
    private String groupCode = "";
    private GridSpacingItemDecoration gridSpacingItemDecoration;

    public InspectionListAdapter(int layoutResId, Activity context, @Nullable List<InspectionPicEntity> data) {
        super(layoutResId, data);
        this.context = context;
        gridSpacingItemDecoration = new GridSpacingItemDecoration(4, Utils.dip2px(context, 2), false);
    }

    @Override
    protected void convert(BaseViewHolder helper, InspectionPicEntity item) {
        TextView tvTitle = helper.getView(R.id.tvTitle);
        RecyclerView rv = helper.getView(R.id.rvView);
        DrawableTextView tvMore = helper.getView(R.id.tvMore);
        tvTitle.setText(item.getTypeName());
        tvMore.setText(item.getImageCount());
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(context, 4);
        rv.setLayoutManager(gridLayoutManager1);
        if (rv.getItemDecorationCount() == 0) {
            rv.addItemDecoration(gridSpacingItemDecoration);
        }
        InspectionAdapter adapter = null;
        int num = Utils.changeNumber(item.getImageCount());
        if (num <= 4) {
            tvMore.setVisibility(View.GONE);
            adapter = new InspectionAdapter(R.layout.item_gallery, context, item.getImagePathList(), 0);
        } else if (num < 8) {
            tvMore.setVisibility(View.VISIBLE);
            adapter = new InspectionAdapter(R.layout.item_gallery, context, item.getImagePathList().subList(0, 4), 0);
        } else {
            tvMore.setVisibility(View.VISIBLE);
            adapter = new InspectionAdapter(R.layout.item_gallery, context, item.getImagePathList().subList(0, 8), 0);
        }
        rv.setAdapter(adapter);
        tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, InspectionGalleryListActivity.class);
                intent.putExtra("data", tvTitle.getText().toString());
                intent.putExtra("groupCode", groupCode);
                intent.putExtra("createTime", item.getTypeName());
                context.startActivity(intent);

            }
        });
    }

    public void setGroupCode(String groupCode) {
        this.groupCode = groupCode;
    }
}
